package portal.monitor.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import portal.monitor.dao.ModeRecord;
import portal.monitor.dao.ModeRecordExample;
import portal.monitor.dao.mapper.ModeRecordMapper;
import portal.monitor.service.ModeRecordService;

@Service("modeRecordService")
public class ModeRecordServiceImpl implements ModeRecordService {

	@Autowired
	public ModeRecordMapper modeMapper;
	
	@Override
	public int add(String IPS,String startType) {
		// TODO Auto-generated method stub
		String[] ips = IPS.split(",");
		int count = 0;
		for(int i=0;i<ips.length;i++){
			ModeRecord mr = new ModeRecord();
			mr.setHostip(ips[i]);
			mr.setModename(startType);
			count += modeMapper.insertSelective(mr);
		}
		return count;
	}

	@Override
	public int delete(String IPS) {
		// TODO Auto-generated method stub
		String[] ips = IPS.split(",");
		int count = 0;
		for(int i=0;i<ips.length;i++){
			count += modeMapper.deleteByIP(ips[i]);
		}
		return count;
	}

	@Override
	public Set<String> getRecord(String IPS) {
		// TODO Auto-generated method stub
		List<String> record = new ArrayList<String>();
		String[] ips = IPS.split(",");
		for(int i=0;i<ips.length;i++){
			record.add(ips[i]);
		}
		ModeRecordExample mre = new ModeRecordExample();
		ModeRecordExample.Criteria criteria = mre.createCriteria();
		criteria.andHostipIn(record);

		List<ModeRecord> list = modeMapper.selectByExample(mre);
		Set<String> modeSet = new HashSet<String>();
		if(list.size()>0){
			for(ModeRecord mr:list){
				modeSet.add(mr.getModename());
			}
		}
		return modeSet;
	}

	
}
